package com.dingpeikeji.quickdeer.modules.wx.realschevehi.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.driver.service.TDriversService;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealscheVehi;
import com.dingpeikeji.quickdeer.modules.ordervehi.entity.TOrderVehi;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;
import com.dingpeikeji.quickdeer.modules.wx.realschevehi.service.WxRealscheVehiService;

/**
 * 实际班次
 */
@Controller
@RequestMapping("${wxPath}/d/realschevehi")
public class WxRealscheVehiController {

	@Autowired
	private WxRealscheVehiService realscheVehiService;
	@Autowired
	private TDriversService tDriversService;
	
	
	/**
	 * 司机发车计划列表
	 * @return
	 */
	@RequestMapping("taskList")
	public String taskList(Model model, HttpServletRequest request){
		TRealscheVehi realscheVehi = new TRealscheVehi();
		// 设置司机
		TDrivers driver = (TDrivers)request.getSession().getAttribute(OAuth.SESSION_DRIVER);
		if (driver == null) {
			TDrivers query = new TDrivers();
			String openid = request.getParameter("wechatid");
			query.setWechatid(openid);
			List<TDrivers> driverList = tDriversService.findList(query);
			if (driverList != null && !driverList.isEmpty()) { // 认证通过
				request.getSession().setAttribute(OAuth.SESSION_DRIVER, driverList.get(0));
				realscheVehi.setMaindriver(driverList.get(0).getDrvssysid());
				List<TRealscheVehi> realscheVehiList = realscheVehiService.findRealscheVehiList(realscheVehi);
				model.addAttribute("realscheVehiList", realscheVehiList);
				return "wx/modules/realschevehi/taskList";
			} else { // 去绑定
				return "redirect:" + Global.getWxPath() + "/bind?openid=" + openid;
			}
		}else{
			realscheVehi.setMaindriver(driver.getDrvssysid());
			List<TRealscheVehi> realscheVehiList = realscheVehiService.findRealscheVehiList(realscheVehi);
			model.addAttribute("realscheVehiList", realscheVehiList);
			return "wx/modules/realschevehi/taskList";
		}
	}
	
	/**
	 * 发车历史
	 * @return
	 */
	@RequestMapping("history")
	public String history(HttpServletRequest request, Model model){
		TRealscheVehi realscheVehi = new TRealscheVehi();
		// 设置司机
		TDrivers driver = (TDrivers)request.getSession().getAttribute(OAuth.SESSION_DRIVER);
		realscheVehi.setMaindriver(driver.getDrvssysid());
		List<TRealscheVehi> realscheVehiList = realscheVehiService.findTaskHistoryList(realscheVehi);
		model.addAttribute("realscheVehiList", realscheVehiList);
		return "wx/modules/realschevehi/taskHistory";
	}
	
	/**
	 * 删除历史
	 */
	@RequestMapping("delete")
	public String delete(TRealscheVehi realscheVehi, RedirectAttributes redirectAttributes) {
		if(realscheVehiService.delete(realscheVehi)){
			redirectAttributes.addFlashAttribute("message","删除历史成功");
		} else {
			redirectAttributes.addFlashAttribute("message","删除历史失败！");
		}
		return "redirect:/wx/d/realschevehi/history";
	}
	
	/**
	 * 实际子班次的订单信息
	 * @return
	 */
	@RequestMapping("order")
	public String order(String realvehiid, Model model){
		List<TOrderVehi> orderVehiList = realscheVehiService.findOrderVehiList(realvehiid);
		model.addAttribute("orderVehiList", orderVehiList);
		model.addAttribute("realvehiid", realvehiid);
		return "wx/modules/realschevehi/order";
	}
	
	/**
	 * 接单
	 */
	@RequestMapping("pick")
	public String pick(TRealscheVehi realscheVehi, RedirectAttributes redirectAttributes) {
		if(realscheVehiService.pick(realscheVehi)){
			redirectAttributes.addFlashAttribute("message","接单成功");
		} else {
			redirectAttributes.addFlashAttribute("message","接单失败，请联系平台！");
		}
		return "redirect:/wx/d/realschevehi/taskList";
	}
	
	/**
	 * 完单
	 */
	@RequestMapping("over")
	public String over(TRealscheVehi realscheVehi, RedirectAttributes redirectAttributes) {
		String message = realscheVehiService.over(realscheVehi);
		if(message == null){
			redirectAttributes.addFlashAttribute("message","完单成功");
		} else {
			redirectAttributes.addFlashAttribute("message", message);
		}
		return "redirect:/wx/d/realschevehi/taskList";
	}
	
	/*@RequestMapping("settle")
	public String settle(TRealscheVehi realscheVehi, Model model) {
		model.addAttribute("realscheVehi", realscheVehi);
		return "wx/modules/realschevehi/settle";
	}
	
	@RequestMapping("saveSettle")
	public String saveSettle(TRealscheVehi realscheVehi, RedirectAttributes redirectAttributes) {
		if(realscheVehiService.saveSettle(realscheVehi)){
			redirectAttributes.addFlashAttribute("message","结算成功");
		} else {
			redirectAttributes.addFlashAttribute("message","结算失败");
		}
		return "redirect:/wx/d/realschevehi/taskList";
	}
	
	@RequestMapping("sure")
	public String sure(TRealscheVehi realscheVehi, RedirectAttributes redirectAttributes) {
		if(realscheVehiService.confirm(realscheVehi)){
			redirectAttributes.addFlashAttribute("message","确认成功");
		} else {
			redirectAttributes.addFlashAttribute("message","确认失败");
		}
		return "redirect:/wx/d/realschevehi/taskList";
	}
	
	*//**
	 * 发车
	 * @return
	 *//*
	@RequestMapping("start")
	public String start(String realvehiid, Model model, RedirectAttributes redirectAttributes){
		boolean result = realscheVehiService.updateRealscheVehi(realvehiid);
		redirectAttributes.addFlashAttribute("message", result?"发车成功":"发车失败");
		return "redirect:"+Global.getWxPath()+"/d/realschevehi/taskList";
	}*/
}
